package com.sc.userspringboot.controller;

import com.sc.userspringboot.pojo.Posts;
import com.sc.userspringboot.service.IPostsService;
import com.sc.userspringboot.service.IUsersService;
import com.sc.userspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.userspringboot.pojo.Posts;
import com.sc.userspringboot.service.IPostsService;
import com.sc.userspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户发布帖子主表 前端控制器
 * </p>
 *
 * @author user
 * @since 2025-09-19 06:25:46
 */
@RestController
@RequestMapping("/posts")
public class PostsController {
    //TODO: 实现帖子相关功能

    @Autowired
    IPostsService postsService;

    //TODO: 实现用户端发布帖子，将帖子信息保存到数据库中
    @RequestMapping("add")
    ResponseMsg add(@RequestBody Posts posts) {
        System.out.println(posts);
        if (posts.getTitle() == null || posts.getContent() == null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "标题/内容不能为空哦~");
        }
        if (posts.getTitle().length() > 50) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "标题不能超过50个字哦~");
        }
        if (posts.getContent().length() > 500) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "内容不能超过500个字哦~");
        }

        if (!postsService.save(posts)) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "发布失败");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "发布成功", posts.getPostId());
    }



    @GetMapping("getUserPosts")
    public ResponseMsg getUserPosts(Integer id) {
        QueryWrapper<Posts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        List<Posts> posts = postsService.list(queryWrapper);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, posts);
    }

}
